package com.fr.report.script.function;

import com.fr.base.FRContext;
import com.fr.base.file.DatasourceManager;
import com.fr.data.core.db.DBUtils;
import com.fr.data.impl.DatabaseConnection;
import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;
import com.fr.report.script.core.FArray;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class RECORDS extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if ((paramArrayOfObject.length < 3) || (paramArrayOfObject.length > 4))
      return Primitive.ERROR_NAME;
    String str1 = paramArrayOfObject[0].toString();
    String str2 = paramArrayOfObject[1].toString();
    Object localObject1 = paramArrayOfObject[2];
    Object localObject2 = null;
    int i = -1;
    if (paramArrayOfObject.length == 4)
    {
      localObject2 = paramArrayOfObject[3];
      if (localObject2 instanceof Integer)
        i = Integer.parseInt(localObject2.toString());
    }
    return records(str1, str2, localObject1, i);
  }

  private Object records(String paramString1, String paramString2, Object paramObject, int paramInt)
  {
    FArray localFArray = new FArray();
    if ((paramObject == null) || (paramString2 == null) || (paramString1 == null))
      return Primitive.NULL;
    com.fr.data.impl.Connection localConnection = FRContext.getDatasourceManager().getConnection(paramString1);
    if ((localConnection == null) || (!(localConnection instanceof DatabaseConnection)))
      return Primitive.NULL;
    DatabaseConnection localDatabaseConnection = (DatabaseConnection)localConnection;
    java.sql.Connection localConnection1 = null;
    try
    {
      localConnection1 = localDatabaseConnection.createConnection();
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    if (!(DBUtils.isExsitedTable((DatabaseConnection)localConnection, paramString2)))
      return Primitive.NULL;
    String str = "";
    String[] arrayOfString = null;
    int i = 0;
    str = "select " + paramObject + " from " + paramString2;
    if (paramObject instanceof Integer)
    {
      i = Integer.parseInt(paramObject.toString());
      str = "select * from " + paramString2;
      arrayOfString = DBUtils.checkInColumnNames(localConnection1, str, 0);
      str = "select " + arrayOfString[(i - 1)] + " from " + paramString2;
    }
    PreparedStatement localPreparedStatement = null;
    try
    {
      localPreparedStatement = localConnection1.prepareStatement(str);
    }
    catch (SQLException localSQLException1)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException1.getMessage(), localSQLException1);
    }
    ResultSet localResultSet = null;
    try
    {
      localResultSet = localPreparedStatement.executeQuery();
    }
    catch (SQLException localSQLException2)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException2.getMessage(), localSQLException2);
    }
    try
    {
      if (paramInt < 0)
        while (true)
        {
          while (true)
          {
            if (!(localResultSet.next()))
              break label428;
            if (!(paramObject instanceof Integer))
              break;
            localObject = localResultSet.getObject(arrayOfString[(i - 1)]);
            localFArray.add(localObject);
          }
          Object localObject = localResultSet.getObject(paramObject.toString());
          localFArray.add(localObject);
        }
      int j = 0;
      do
        if (!(localResultSet.next()))
          break label428;
      while (++j != paramInt);
      if (paramObject instanceof Integer)
        return localResultSet.getObject(arrayOfString[(i - 1)]);
      label428: return localResultSet.getObject(paramObject.toString());
    }
    catch (SQLException localSQLException3)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException3.getMessage(), localSQLException3);
    }
    try
    {
      if (localResultSet != null)
        localResultSet.close();
      if (localPreparedStatement != null)
        localPreparedStatement.close();
      if (localConnection1 != null)
        localConnection1.close();
    }
    catch (SQLException localSQLException4)
    {
      FRContext.getLogger().log(Level.WARNING, localSQLException4.getMessage(), localSQLException4);
    }
    return localFArray;
  }

  public Function.Type getType()
  {
    return Function.REPORT;
  }

  public String getCN()
  {
    return "RECORDS(connection, table,field):返回数据库表table中字段名field下的所有元素。\n示例：\n数据库BASE中有个名叫task的表的内容如下：\nname start  end\na    2008   2009\nb    2009   2012\n那么RECORDS(\"BASE\",\"task\",\"end\")等于[2009,2012].\n\nRECORDS(connection, table,field,row)返回table中field字段下的第row行的记录，field可以为列名也可以为列号。\n    RECORDS(\"BASE\",\"task\",\"end\",2)等于2012.\n    RECORDS(\"BASE\",\"task\",2,2)等于2009.";
  }

  public String getEN()
  {
    return "";
  }
}